<?php
namespace app\passenger\model;

use app\BaseModel;
use think\facade\Db;

class AdminActive extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_passenger_active';

    protected $append = [

      'status_text'
    ];


    /**
     * @param $value
     * @param $data
     * @功能说明:
     * @author chenniang
     * @DataTime: 2020-03-21 15:00
     */
    public function getStatusTextAttr($value,$data){

        if(!empty($data['start_time'])&&$data['end_time']) {
            if ($data['start_time'] > time()) {

                return 1;

            } elseif ($data['start_time'] < time() && $data['end_time'] > time()) {

                return 2;

            } else {

                return 3;
            }
        }

    }


    /**
     * @param $value
     * @param $data
     * @功能说明:转换图片格式
     * @author chenniang
     * @DataTime: 2020-03-09 18:56
     */
    public function getImgsAttr($value){

        $data = transImages([$value],['imgs']);

        return !empty($data[0])?$data[0]:[];

    }


    /**
     * @param $value
     * @功能说明:封面图
     * @author chenniang
     * @DataTime: 2020-03-09 19:02
     */
    public function getCoverAttr($value,$data){

        $img = transImagesOne([$value],['cover'],$data['uniacid']);

        return !empty($img[0])?$img[0]:'';
    }

    /**
     * @param $dis
     * @param $page
     * @功能说明:活动列表
     * @author chenniang
     * @DataTime: 2020-03-09 18:42
     */

    public function activeList($dis,$page){

        $data = $this->where($dis)->order(['id desc'])->paginate($page)->toArray();

        return $data;
    }


    /**
     * User: chenniang
     * Date: 2019-12-18 14:11
     * @return void
     * descrption:活动详情
     */
    public function activeInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];
    }

    /**
     * User: chenniang
     * Date: 2019-12-18 14:11
     * @return void
     * descrption:添加活动
     */
    public function activeAdd($data){
        //开始时间
        $data['create_time'] = time();
        //更新时间
        $data['update_time'] = time();

        $data['status']      = 1;

        if(!empty($data['company'])){

            $company = $data['company'];
            unset($data['company']);
        }

        $res = $this->insert($data);
        //获取id
        $id  = $this->getLastInsID();
        //公司
        $company_model = new AdminCompany();
        //添加到每个公司
        if(!empty($company)){

            foreach ($company as $v){

                $insert = [
                    //uniacid
                    'uniacid'    => $data['uniacid'],
                    //公司id
                    'company_id' => $v,
                    //活动id
                    'active_id'  => $id
                ];
               $res = $company_model->companyAdd($insert);
            }
        }

        return $res;
    }

    /**
     * User: chenniang
     * Date: 2019-12-18 14:13
     * @return void
     * descrption:编辑活动
     */
    public function activeUpdate($dis,$data){
        //更新时间
        $data['update_time'] = time();

        //添加到每个公司
        if(!empty($data['company'])){
            //公司
            $company_model = new AdminCompany();
            //全部删除
            $company_model->companyDel(['active_id'=>$data['id']]);

            foreach ($data['company'] as $v){

                $insert = [
                    //uniacid
                    'uniacid'    => $data['uniacid'],
                    //公司id
                    'company_id' => $v,
                    //活动id
                    'active_id'  => $data['id']
                ];
                $res = $company_model->companyAdd($insert);
            }
            unset($data['company']);
        }
        $res = $this->where($dis)->update($data);

        return $res;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-03-21 15:04
     * @功能说明:正在进行的活动
     */
    public function activeIng($uniacid,$id = ''){

        $dis[] = ['start_time','<',time()];

        $dis[] = ['end_time','>',time()];

        $dis[] = ['status','=',1];

        $dis[] = ['uniacid','=',$uniacid];

        if(!empty($id)){

            $dis[] = ['id','<>',$id];
        }

        $data = $this->where($dis)->find();

        return $data;

    }












}